home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-02-08 | 6.2 KB | 286 lines | [TEXT/KEEN] |
- # XRef.T (or .A): from one or more source files, build a cross-reference
- # list of selected top-level terms.
- #
- ############## NOT AN EXECUTABLE FILE ###################
- #
- #this file is used only as a "Template" by
- #$XRef, which is the one to call for cross-referencing.
- #The file "Skip for XRef" below holds a list of common terms to skip over.
- #In large cross-reference jobs it may be NECESSARY to skip the commonest
- #terms to avoid overflowing the list of references.
- #
- #The tedious bit: stripping comments and strings (and character constants).
- # A line can begin within string or comment, contain several of each,
- # and end in the midst of a string or comment - whole line can
- # be within a single string or comment for that matter.
- # inCom == we are in a comment
- # inStr == in a string
-
- BEGIN {
- skipfile = STDPATH "Drag_on Modules:hAWK programs:" "Skip for XRef"
- while (getline < skipfile > 0)
- {
- for ( k = 1; k <= NF; k++)
- skipList[$k] = 1; #Forces skipList[$k] to "exist".
- }
- close(skipfile)
- $0 = ""
- progressFile = STDPATH "$tempProgress"
- }
-
- FNR == 1 {
- if (inCom || inStr)
- {
- if (inCom)
- errString = "Unbalanced comment in " FILENAME " - please fix."
- else
- errString = "Unbalanced string in " FILENAME " - please fix."
- do_exit()
- }
- z = split(FILENAME, names, ":")
- fName[++fIndex] = names[z]
- if (!progress("\nXReffing: " names[z]))
- { # concurrent mode, print progress to file
- print "XReffing:", names[z] > progressFile
- close(progressFile)
- }
- }
-
- {
- if (inCom) #starting in comment
- SkipWhileInComment(); #or stop at end of line
- else if (inStr) #starting in string
- SkipWhileInString(); #or stop at end of line
- if (!inCom && !inStr &&
- (index($0, "/*") || index($0, "\"") || index($0, "'")))
- SkipComsAndStrs(); # - and ticks, or stop at end of line
- #finally, strip C++-style line-end comments
- if (!inCom && !inStr && (startPlus = index($0, "//")))
- $0 = substr($0, startPlus-1)
- }
-
- #Strip down to words, look up term and record locations.
- #Only the file index is recorded - at end when printing, the index
- #is replaced by the proper file name.
- {
- gsub(/[^A-Za-z0-9_]/, " ")
- for (i = 1; i <= NF; ++i)
- {
- #skip if in skipList
- if ($i in skipList)
- continue;
- type = lookup($i)
- ##MARK record
- #if (type == 1) #define
- # defines[$i] = defines[$i] "\t" fIndex "\t" FNR
- #else if (type == 2)#variable
- # vars[$i] = vars[$i] "\t" fIndex "\t" FNR
- #etc
- }
- }
-
- END {
- if (noGo == 1)
- exit;
- if (!progress("\nBuilding final list..."))
- { # concurrent mode, print progress to file
- print "Building final list..." > progressFile
- close(progressFile)
- }
- print "Cross-reference listing:"
- #build master array, for sorting all at once
- ##MARK linear
- #for (w in defines)
- # {
- # linear[++m] = w ": #define" defines[w]
- # delete defines[w]
- # }
- #for (w in vars)
- # {
- # linear[++m] = w ": variable" vars[w]
- # delete vars[w]
- # }
- #etc
- if (!progress("\nSorting final list..."))
- { # concurrent mode, print progress to file
- print "Sorting final list..." > progressFile
- close(progressFile)
- }
- sort(linear, ind, "d")
- if (!progress("\nPrinting final list to stdout..."))
- { # concurrent mode, print progress to file
- print "Printing final list to stdout..." > progressFile
- close(progressFile)
- }
- for (j = 1; j <= m; ++j)
- {
- r = split(linear[ind[j]], out, "\t")
- print out[1]
- for (t = 2; t <= r; ++t)
- {#print file name from array fName, print line number
- print "\t", fName[out[t]], out[++t]
- }
- print ""
- }
- if (m == 0)#nothing at all
- print "No top-level terms were found."
- }
-
- function SkipWhileInComment( len, i)
- {
- len = length()#note $0 is default argument for length()
- for (i = 1; i <= len && inCom; ++i)
- {
- if (substr($0,i,1) == "*" && substr($0,i+1,1) == "/")
- inCom = 0;
- }
- if (inCom)#comment continues on next line
- $0 = ""
- else #strip out the comment from the start of $0
- $0 = substr($0,i+2)
- }
-
- function SkipWhileInString( len, i, c, esc)
- {
- esc = 0
- len = length()
- for (i = 1; i <= len && inStr; ++i)
- {
- c = substr($0,i,1)
- if (c == "\"")
- {
- if (esc == 0 || esc%2 == 0)
- inStr = 0;
- else
- esc = 0
- }
- else if (c == "\\")
- ++esc
- else
- esc = 0
- }
- if (inStr)
- {
- if (c != "\\")
- {
- errString = "Unbalanced string at " FILENAME " " FNR " - please fix."
- do_exit()
- }
- $0 = ""
- }
- else
- $0 = substr($0,i+1)
- }
-
- #We know we're not starting within a comment or string,
- #and line may contain comment or string start.
- function SkipComsAndStrs( c, esc, i, len)
- {
-
- len = length()
- for (i = 1; i <= len && !inCom && !inStr; ++i)
- {
- c = substr($0,i,1)
- if (c == "/" && substr($0,i+1,1) == "*")
- {
- inCom = 1;
- len -= StripComment(i, len);
- }
- else if (c == "\"")
- {
- inStr = 1;
- len -= StripString(i, len);
- }
- else if (c == "'")
- {
- len -= StripTicks(i, len);
- }
- }
- }
-
- function StripComment(start, len, i)
- {
- for (i = start+2; i <= len && inCom; ++i)
- {
- if (substr($0,i,1) == "*" && substr($0,i+1,1) == "/")
- inCom = 0;
- }
- if (inCom)#comment continues on next line
- $0 = substr($0,1,start-1)
- else #strip out the comment from the middle of $0
- $0 = substr($0,1,start-1) substr($0,i+2)
- return i - start + 2
- }
-
- function StripString(start, len, i,c,esc)
- {
- esc = 0
- for (i = start+1; i <= len && inStr; ++i)
- {
- c = substr($0,i,1)
- if (c == "\"")
- {
- if (esc == 0 || esc%2 == 0)
- inStr = 0;
- else
- esc = 0
- }
- else if (c == "\\")
- ++esc
- else
- esc = 0
- }
- if (inStr)
- {
- if (c != "\\")
- {
- errString = "Unbalanced string at " FILENAME " " FNR " - please fix."
- do_exit()
- }
- $0 = substr($0,1,start-1)
- }
- else
- $0 = substr($0,1,start-1) substr($0,i+1)
- return i - start + 1
- }
-
- function StripTicks(start, len, i,c,esc)
- {
- esc = 0
- for (i = start+1; i <= len; ++i)
- {
- c = substr($0,i,1)
- if (c == "'")
- {
- if (esc == 0 || esc%2 == 0)
- break;
- else
- esc = 0
- }
- else if (c == "\\")
- ++esc
- else
- esc = 0
- }
- if (i > len)
- {
- errString = "Unbalanced ticks at " FILENAME " " FNR " - please fix."
- do_exit()
- }
- $0 = substr($0,1,start-1) substr($0,i+1)
- return i - start + 1
- }
-
- function do_exit()
- {
- prompt(errString)
- print ""
- print ""
- print "------------ left off at ----------"
- print
- print "--- " errString
- print errString > "stderr"
- noGo = 1
- exit
- }
-